// import Cookies from 'js-cookie'
import { encrypt, decrypt } from "@/utils/crypto"
const EN_KEY = "0A1B2C3D4E5F6789"
const EN_IV = "0123F4E5D6C7B8A9"

const TokenKey = 'token'
const CurrentUserKey = 'currentUser'
const RoutersKey = 'routers'
const SysSettingKey = 'sysSetting'
const SysSecurityKey = 'sysSecurity'
const DataPermisionsKey = 'dataPermisions'
const CurrentCompanyKey = 'currentCompany'

let token = null
let currentUser = null
let routers = null
let setting = null
let security = null
let dataPermisions = null
let currentCompany = null

// 清除本地所有数据
export function clearLocalData() {
  token = null
  currentUser = null
  routers = null
  setting = null
  security = null
  dataPermisions = null
  currentCompany = null
  sessionStorage.clear()
}

const getEncryptItem = (key) => {
  let local = sessionStorage.getItem(key)
  return local ? decrypt(local, EN_KEY, EN_IV) : local
}

const setEncryptItem = (key, value) => {
  sessionStorage.setItem(key, encrypt(value, EN_KEY, EN_IV))
}

// 获得token
export function getLocalToken() {
  if(!token) {
    token = getEncryptItem(TokenKey)
  }
  return token
}
// 保存token
export function setLocalToken(token) {
  setEncryptItem(TokenKey, token)
}

// 清除tocken
export function removeLocalToken() {
  token = null
  sessionStorage.removeItem(TokenKey)
}

// 获得登录用户
export function getLocalCurrentUser() {
  if(!currentUser) {
    currentUser = JSON.parse(getEncryptItem(CurrentUserKey))
  }
  return currentUser
}
// 保存登录用户
export function setLocalCurrentUser(currentUser) {
  setEncryptItem(CurrentUserKey, JSON.stringify(currentUser))
}

// 获取登录用户的路由
export function getLocalRouters() {
  if(!routers) {
    routers = JSON.parse(getEncryptItem(RoutersKey))
  }
  return routers
}
// 保存登录用户路由
export function setLocalRouters(routers) {
  setEncryptItem(RoutersKey, JSON.stringify(routers))
}

// 获取系统设置
export function getLocalSysSetting() {
  if(!setting) {
    setting = JSON.parse(getEncryptItem(SysSettingKey))
  }
  return setting
}
// 保存系统设置
export function setLocalSysSetting(settings) {
  setEncryptItem(SysSettingKey, JSON.stringify(settings))
}

// 获取安全策略
export function getLocalSysSecurity() {
  if(!security) {
    security = JSON.parse(getEncryptItem(SysSecurityKey))
  }
  return security
}
// 保存安全策略
export function setLocalSysSecurity(security) {
  setEncryptItem(SysSecurityKey, JSON.stringify(security))
}


// 获取登录用户数据权限
export function getLocalDataPermisions() {
  if(!dataPermisions) {
    dataPermisions = JSON.parse(getEncryptItem(DataPermisionsKey))
  }
  return dataPermisions
}
// 保存登录用户数据权限
export function setLocalDataPermisions(dataPermission) {
  setEncryptItem(DataPermisionsKey, JSON.stringify(dataPermission))
}

// 获取登录用户公司
export function getLocalCurrentCompany() {
  if(!currentCompany) {
    currentCompany = JSON.parse(getEncryptItem(CurrentCompanyKey))
  }
  return currentCompany
}
// 保存登录用户公司
export function setLocalCurrentCompany(currentCompany) {
  setEncryptItem(CurrentCompanyKey, JSON.stringify(currentCompany))
}
